﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using NGE;

namespace MH.SE
{
    public partial class SceneInfo : UserControl
    {
        //Scene ((SceneComponent)game.GetComponent("SceneCOM")).SCN;
        Game game;

        public int SelectedLayer;
        public int CodeLayer;
        public bool EditingCode
        {
            get
            {
                if (tabControl1.SelectedIndex == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

       public SceneInfo()
        {
            InitializeComponent();
        }

        private void SceneInfo_Load(object sender, EventArgs e)
        {
            refresh();
        }

        public void LoadScene(Game g)
        {
            game = g;
            refresh();
            checkedListBox1.SelectedIndex = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Terrain.Layers.Count - 1;
        }

        public void refresh()
        {
            if (game == null)
            {
                return;
            } if (((SceneComponent)game.GetComponent("SceneCOM")).SCN == null)
            {
                return;
            }

            textBox1.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Name;
            textBox2.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MusicFileName;
            textBox3.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.FrameInterval.ToString();

            textBox4.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Size.Width.ToString();
            textBox5.Text = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Size.Height.ToString();

            checkedListBox1.Items.Clear();
            for (int i = 0; i < ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Terrain.Layers.Count; i++)
            {
                checkedListBox1.Items.Insert(0, i);
                checkedListBox1.SetItemChecked(0, ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Terrain.Layers[i].Visible);
            }
            checkedListBox1.SelectedIndex = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Terrain.Layers.Count - 1;
            listBox1.SelectedIndex = 0;
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = checkedListBox1.SelectedIndex;
            if (i >= 0)
            {
                SelectedLayer = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Terrain.Layers.Count - i - 1;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Name = textBox1.Text;
            ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MusicFileName = textBox2.Text;
            ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Size = new Size(int.Parse(textBox4.Text), int.Parse(textBox5.Text));
            ((SceneComponent)game.GetComponent("SceneCOM")).SCN.FrameInterval = int.Parse(textBox3.Text);
        }

        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue.ToString() == "Unchecked")
            {
                ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Terrain.Layers[SelectedLayer].Visible = false;
            }
            else
            {
                ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Terrain.Layers[SelectedLayer].Visible = true;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                game.Music.Load(game.Option.SoundPath + ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MusicFileName);
                game.Music.Play();
            }
            else
            {
                game.Music.Stop();
            }
        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "NGE Sound|*.nsnd|mp3|*.mp3";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                if (fd.FilterIndex == 1)
                {
                    ((SceneComponent)game.GetComponent("SceneCOM")).SCN.MusicFileName = System.IO.Path.GetFileName(fd.FileName);
                    refresh();
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CodeLayer = listBox1.SelectedIndex;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int i = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Terrain.Layers[SelectedLayer].GetTileIndex(Util.Point2Vector3(((SECOM)game.Components[game.Components.Count - 1]).CursorSelectedRect.Location));
            ((SceneComponent)game.GetComponent("SceneCOM")).SCN.ExtendTile = (Terrain.Layer.Tile)Util.Clone(((SceneComponent)game.GetComponent("SceneCOM")).SCN.Terrain.Layers[SelectedLayer].Tiles[i]);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            CodeEditor ce = new CodeEditor();
            ce.CODE = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.LoadScript;
            if (ce.ShowDialog() == DialogResult.OK)
            {
                ((SceneComponent)game.GetComponent("SceneCOM")).SCN.LoadScript = ce.CODE;
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            CodeEditor ce = new CodeEditor();
            ce.CODE = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.UnloadScript;
            if (ce.ShowDialog() == DialogResult.OK)
            {
                ((SceneComponent)game.GetComponent("SceneCOM")).SCN.UnloadScript = ce.CODE;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Terrain.Layers.Insert(SelectedLayer, new Terrain.Layer());
            refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ((SceneComponent)game.GetComponent("SceneCOM")).SCN.Terrain.Layers.RemoveAt(SelectedLayer);
            refresh();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for(int i=0;i<((SceneComponent)game.GetComponent("SceneCOM")).SCN.TileCodeLayer.TileCodeSet.Count;i++)
            {
                ((SceneComponent)game.GetComponent("SceneCOM")).SCN.TileCodeLayer.TileCodeSet[i].MTL = ((SceneComponent)game.GetComponent("SceneCOM")).SCN.TileCodeLayer.TileCodeSet[i].CHR;
            }
        }
    }
}
